/*
 * Console.cpp
 *
 *  Created on: May 30, 2011
 *      Author: madboyka
 */

#include "Console.h"
#include <time.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
Console *Console::instance;

Console::Console()
{
#ifdef USE_CURSES
	logWindow = 0;
	debugWindow = 0;
	statusWindow = 0;
	//	int startx, starty;
	//	int ch;
	initscr();
	//	if (has_colors() == false)
	//	{
	//		endwin();
	//		printf("Your terminal does not support color\n");
	//	}
	//	else
	//	{
	//		start_color(); /* Start color                  */
	//		init_pair(1, COLOR_RED, COLOR_BLACK);
	//	}

	//	clear();
	//	cbreak();
	//	nl();
	//	logWindow = stdscr;
	//	noecho();
	//	keypad(stdscr, TRUE);
	height = getmaxy(stdscr);
	width = getmaxx(stdscr);

	logWindow = newwin(height - 10, width, 10, 0);
	scrollok(logWindow, true);
	wrefresh(logWindow);
	wclear(logWindow);
	wrefresh(logWindow);

	statusWindow = newwin(1, width, 0, 0);
	wrefresh(statusWindow);
	wmove(statusWindow, 0, 0);
	wprintw(statusWindow, "Robots daemon v 0.9.9.9");
	wrefresh(statusWindow);

	debugWindow = newwin(9, width, 1, 0);
	wrefresh(debugWindow);

	curs_set(0);
	refresh();
#endif
	//	endwin();
}

void Console::close()
{
#ifdef USE_CURSES
	endwin();
#endif
}

void Console::waitForInput()
{
#ifdef USE_CURSES
	getch();
#endif
}

Console* Console::init()
{
	instance = new Console();
	return instance;
}

void Console::log(const char* format, ...)
{
#ifdef USE_CURSES
	if (logWindow != 0)
	{
		time_t timestamp = time(NULL);
		char stime[9];
		strftime(stime, 9, "[%H:%M:%S]", localtime(&timestamp));
		//		int len = strlen(message);
		//		char buffer[(len + 8 + 5)];
		wprintw(logWindow,stime);
		va_list argptr;
		va_start(argptr,format);
		vwprintw(logWindow, format, argptr);
		va_end(argptr);
		waddch(logWindow,'\n');

		//		time_t timestamp = time(NULL);
		//		char stime[9];
		//		strftime(stime, 9, "%H:%M:%S", localtime(&timestamp));
		//		int len = strlen(message);
		//		char buffer[(len + 8 + 5)];
		//		sprintf(buffer, "[%s]%s\n", stime, message);

		wrefresh(logWindow);
	}
#endif
#ifndef USE_CURSES
	time_t timestamp = time(NULL);
			char stime[9];
			strftime(stime, 9, "%H:%M:%S", localtime(&timestamp));
			printf("[%s] ",stime);
	va_list argptr;
	va_start(argptr,format);
	vprintf(format, argptr);
	va_end(argptr);
	puts("");
#endif
}

void Console::logput(const char *message)
{
#ifdef USE_CURSES
	if (logWindow != 0)
	{
		time_t timestamp = time(NULL);
		char stime[9];
		strftime(stime, 9, "%H:%M:%S", localtime(&timestamp));
		int len = strlen(message);
		char buffer[(len + 8 + 5)];
		sprintf(buffer, "[%s]%s\n", stime, message);
		//		int mx = getmaxx(logWindow);
		//		int my = getmaxy(logWindow);
		//		int y = getcury(logWindow);
		//		wmove(logWindow,0,y+1);

		wprintw(logWindow, buffer);
		//		logf("%d %f noice fomattig.!\n", 12, 234.567);
		//		printw(buffer);

		//		wprintw(logWindow,"%d %d is the size\n",mx,my);
		//		wclrtoeol(logWindow);
		wrefresh(logWindow);
		//		refresh();
		//		getch();
		//		puts(buffer);
	}
#endif
#ifndef USE_CURSES
	puts(message);
#endif
}

void Console::dumpMatrix(float* m)
{
#ifdef USE_CURSES
	if (debugWindow != 0)
	{
		if (m == 0)
		{
			log("The matrix is null");
			return;
		}
		//		wclear(debugWindow);
		wmove(debugWindow, 0, 0);
		for (int i = 0; i < 4; ++i)
		{
			wprintw(debugWindow, "|");
			for (int j = 0; j < 4; ++j)
			{
				wprintw(debugWindow, "%2.2f ", m[i * 4 + j]);
			}
			wprintw(debugWindow, "|\n");
		}
		wprintw(debugWindow, "\n");
		wrefresh(debugWindow);
	}
#endif
}
